﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tssipl.PaymentAdapter.Core;
using Tssipl.PaymentAdapter.MessageFormat.Sfms;

namespace Tssipl.PaymentAdapter.MessageEntity.Sfms.Neft
{
    public class NeftCreditTransactionGroupItem
    {
        public string TRN { get; set; }//16x //M
        public string RelatedReferenceNumber { get; set; }//16x
        public string SendingBranchIFSC { get; set; }//11!x        //M 
        public string SendingCustomerAcType { get; set; }//2!c 
        public string SendingCustomerAcNo { get; set; }//35x  //M
        public string SendingCustomerAcName { get; set; }//50x //M
        public string OriginatorOfRemittance { get; set; }//4*35x  //M
        public string BeneficiaryBranchIFSC { get; set; }//11!x    //M     
        public string BeneficiaryCustomerAcType { get; set; }//2!c 
        public string BeneficiaryCustomerAcNo { get; set; }//35x  //M
        public string BeneficiaryCustomerAcName { get; set; }//50x //M
        public string BeneficiaryCustomerAddress { get; set; }//4*35x  
        public string RemittanceInformation { get; set; }//6*35x 
        public string ReasonCode { get; set; }//3!x
        public string RejectionReason { get; set; }//50x
        public string Amount { get; set; }//19d //M
        public string ValueDate { get; set; }//8!n //M
        public string RemittanceDate { get; set; }//8!n //M        
    }
    public class NeftCreditTransaction : Tssipl.PaymentAdapter.MessageEntity.Sfms.SfmsMessageEntityBase
    {
        public SfmsBlock4 block4 { get; set; }

        public string TRN { get; set; }//16x //M
        public string BatchTime { get; set; }        //4!n  //M
        public string SettlementAccountNumber { get; set; }        //35x //M
        public string TotalNoOfTransactions { get; set; }//8n  //M
        public string TotalAmount { get; set; }//19d  //M
        public List<NeftCreditTransactionGroupItem> GroupItemList { get; set; }

        public NeftCreditTransaction(MessageParseResults results)
            : base(results)
        {
            SfmsMessage message = (SfmsMessage)results.getMessage();

            block4 = message.getBlock4();
            this.TRN = block4.getField("2020").Trim();

            this.BatchTime = block4.getField("3535");
            this.TotalNoOfTransactions = block4.getField("5180");
            this.TotalAmount = block4.getField("4110");

            //block4.getFields()
            List<Field> block4FieldList = block4.getFields();
            //foreach (SfmsField field in block4.getFields())
            this.GroupItemList = new List<NeftCreditTransactionGroupItem>();
            NeftCreditTransactionGroupItem groupItem = null;
            for (int i = 4; i < block4FieldList.Count; i++)
            {
                if (block4FieldList[i].getTag() == "2020")
                {
                    if (groupItem != null)
                    {
                        this.GroupItemList.Add(groupItem);
                    }
                    groupItem = new NeftCreditTransactionGroupItem();
                    groupItem.TRN = block4FieldList[i].getValue();
                }
                if (block4FieldList[i].getTag() == "2006")
                    groupItem.RelatedReferenceNumber = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "5756")
                    groupItem.SendingBranchIFSC = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6305")
                    groupItem.SendingCustomerAcType = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6021")
                    groupItem.SendingCustomerAcNo = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6091")
                    groupItem.SendingCustomerAcName = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "7002")
                    groupItem.OriginatorOfRemittance = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "5569")
                    groupItem.BeneficiaryBranchIFSC = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6310")
                    groupItem.BeneficiaryCustomerAcType = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6061")
                    groupItem.BeneficiaryCustomerAcNo = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6081")
                    groupItem.BeneficiaryCustomerAcName = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "5565")
                    groupItem.BeneficiaryCustomerAddress = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "7495")
                    groupItem.RemittanceInformation = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6346")
                    groupItem.ReasonCode = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "6366")
                    groupItem.RejectionReason = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "4038")
                    groupItem.Amount = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "3380")
                    groupItem.ValueDate = block4FieldList[i].getValue();
                if (block4FieldList[i].getTag() == "3375")
                    groupItem.RemittanceDate = block4FieldList[i].getValue();

            }
            if (groupItem != null)
            {
                this.GroupItemList.Add(groupItem);
            }
        }

        new public string Build()
        {
            block4 = new SfmsBlock4();

            // this.TRN = block4.getField("2020").Trim();
            block4.Fields.Add(new Field("2020", this.TRN));

            //this.BatchTime = block4.getField("3535");
            if (string.IsNullOrEmpty(this.BatchTime) == false) block4.Fields.Add(new Field("3535", this.BatchTime));
            //this.TotalNoOfTransactions = block4.getField("5180");
            if (string.IsNullOrEmpty(this.TotalNoOfTransactions) == false) block4.Fields.Add(new Field("5180", this.TotalNoOfTransactions));
            //this.TotalAmount = block4.getField("4110"); 
            if (string.IsNullOrEmpty(this.TotalAmount) == false) block4.Fields.Add(new Field("4110", this.TotalAmount));

            foreach (NeftCreditTransactionGroupItem groupItem in this.GroupItemList)
            {
               // NeftCreditTransactionGroupItem groupItem = this.GroupItemList[0];

                if (string.IsNullOrEmpty(groupItem.TRN) == false) block4.Fields.Add(new Field("2020", groupItem.TRN));
                //if (block4FieldList[i].getTag() == "2006") groupItem.RelatedReferenceNumber = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.RelatedReferenceNumber) == false) block4.Fields.Add(new Field("2020", groupItem.RelatedReferenceNumber));
                //if (block4FieldList[i].getTag() == "5756") groupItem.SendingBranchIFSC = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.SendingBranchIFSC) == false) block4.Fields.Add(new Field("5756", groupItem.SendingBranchIFSC));
                //if (block4FieldList[i].getTag() == "6305") groupItem.SendingCustomerAcType = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.SendingCustomerAcType) == false) block4.Fields.Add(new Field("6305", groupItem.SendingCustomerAcType));
                //if (block4FieldList[i].getTag() == "6021") groupItem.SendingCustomerAcNo = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.SendingCustomerAcNo) == false) block4.Fields.Add(new Field("6021", groupItem.SendingCustomerAcNo));
                //if (block4FieldList[i].getTag() == "6091") groupItem.SendingCustomerAcName = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.SendingCustomerAcName) == false) block4.Fields.Add(new Field("6091", groupItem.SendingCustomerAcName));
                //if (block4FieldList[i].getTag() == "7002") groupItem.OriginatorOfRemittance = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.OriginatorOfRemittance) == false) block4.Fields.Add(new Field("7002", groupItem.OriginatorOfRemittance));
                //if (block4FieldList[i].getTag() == "5569") groupItem.BeneficiaryBranchIFSC = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.BeneficiaryBranchIFSC) == false) block4.Fields.Add(new Field("5569", groupItem.BeneficiaryBranchIFSC));
                //if (block4FieldList[i].getTag() == "6310") groupItem.BeneficiaryCustomerAcType = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.BeneficiaryCustomerAcType) == false) block4.Fields.Add(new Field("6310", groupItem.BeneficiaryCustomerAcType));
                //if (block4FieldList[i].getTag() == "6061") groupItem.BeneficiaryCustomerAcNo = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.BeneficiaryCustomerAcNo) == false) block4.Fields.Add(new Field("6061", groupItem.BeneficiaryCustomerAcNo));
                //if (block4FieldList[i].getTag() == "6081") groupItem.BeneficiaryCustomerAcName = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.BeneficiaryCustomerAcName) == false) block4.Fields.Add(new Field("6081", groupItem.BeneficiaryCustomerAcName));
                //if (block4FieldList[i].getTag() == "5565") groupItem.BeneficiaryCustomerAddress = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.BeneficiaryCustomerAddress) == false) block4.Fields.Add(new Field("5565", groupItem.BeneficiaryCustomerAddress));
                //if (block4FieldList[i].getTag() == "7495") groupItem.RemittanceInformation = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.RemittanceInformation) == false) block4.Fields.Add(new Field("7495", groupItem.RemittanceInformation));
                //if (block4FieldList[i].getTag() == "6346") groupItem.ReasonCode = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.ReasonCode) == false) block4.Fields.Add(new Field("6346", groupItem.ReasonCode));
                //if (block4FieldList[i].getTag() == "6366") groupItem.RejectionReason = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.RejectionReason) == false) block4.Fields.Add(new Field("6366", groupItem.RejectionReason));
                //if (block4FieldList[i].getTag() == "4038")groupItem.Amount = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.Amount) == false) block4.Fields.Add(new Field("4038", groupItem.Amount));
                //if (block4FieldList[i].getTag() == "3380")groupItem.ValueDate = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.ValueDate) == false) block4.Fields.Add(new Field("3380", groupItem.ValueDate));
                //if (block4FieldList[i].getTag() == "3375")groupItem.RemittanceDate = block4FieldList[i].getValue();
                if (string.IsNullOrEmpty(groupItem.RemittanceDate) == false) block4.Fields.Add(new Field("3375", groupItem.RemittanceDate));

            }

            return base.Build() + block4.getFormattedBlock();
        }
    }
}
